import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 引入动态路由模块
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import settingsRouter from './modules/settings'
import socialRouter from './modules/social_securitys'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import approvalsRouter from './modules/approvals'
import permissionsRouter from './modules/permissions'

// 所有动态路由规则
export const asyncRoutes = [
  departmentsRouter, // 组织架构
  employeesRouter, // 员工
  settingsRouter, // 公司设置
  permissionsRouter, // 权限管理
  socialRouter, // 社保
  attendancesRouter, // 考勤
  salarysRouter, // 工资
  approvalsRouter // 审批
]

// 所有静态路由规则
export const constantRoutes = [
  // login
  {
    path: '/login',
    component: () => import(/* webpackChunkName:"login"*/'@/views/login/index'),
    hidden: true
  },
  // 404
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // dashboard首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  // 导入导出
  {
    path: '/import',
    component: Layout,
    hidden: true, // 不显示到左侧菜单
    children: [{
      path: '',
      name: 'import',
      component: () => import('@/views/import/index.vue')
    }]
  }
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    // 动态路由和静态路由合并
    routes: [...constantRoutes]
  })

const router = createRouter()

// 重置路由 -> 将路由重新实例化，相当于换了一个新的路由,退出时调用该方法
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重新设置路由的可匹配路径
}

export default router
